package cn.lds.j375.healthexam.controller;

import cn.lds.j375.healthexam.condition.AppointmentRecordCondition;
import cn.lds.j375.healthexam.dto.ResultDTO;
import cn.lds.j375.healthexam.entity.AppointmentRecord;
import cn.lds.j375.healthexam.entity.User;
import cn.lds.j375.healthexam.exception.SysException;
import cn.lds.j375.healthexam.service.AppointmentRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("appointment_record")
public class AppointmentRecordController {

    private Logger log = LoggerFactory.getLogger(AppointmentRecordController.class);

    @Autowired
    private AppointmentRecordService appointmentRecordService;

    @GetMapping("appoint_list")
    public String showRecord(Model model, AppointmentRecordCondition condition) throws SysException {

        log.info("condition:{}",condition);
        condition.setUser(new User());

        model.addAttribute("result",appointmentRecordService.selectAppointMentRecordAll(condition));

        return "show_record";
    }

    @GetMapping("/my_appoint")
    public String showMyRecord(Model model,AppointmentRecordCondition condition,HttpSession session) throws SysException {
        User loginUser = (User) session.getAttribute("loginUser");
        condition.setUser(loginUser);
        ResultDTO<List<AppointmentRecord>> listResultDTO = appointmentRecordService.selectAppointMentRecordAll(condition);
        model.addAttribute("result",listResultDTO);
        listResultDTO.getObj().forEach(a->{
            log.info("预约记录:{}",a);
        });
        return "show_my_record";
    }

    @GetMapping("/show_record_desc")
    public String showRecordDesc(Integer id,Model model) throws SysException {

        log.info("showRecordDesc方法运行");
        ResultDTO<AppointmentRecord> appointmentRecordResultDTO = appointmentRecordService.selectAppointMentRecordById(id);
        model.addAttribute("result",appointmentRecordResultDTO);

        return "record_desc";

    }

    @GetMapping("appoint_health")
    public String addAppoint(Integer examPackageId,Model model){

        log.info("要预约的套餐为:{}",examPackageId);

        model.addAttribute("examPackageId",examPackageId);


        return "appoint";
    }

    @GetMapping("do_appoint")
    public String doAppoint(AppointmentRecord ar, HttpSession session) throws SysException {
        User loginUser = (User) session.getAttribute("loginUser");
        ar.setUserId(loginUser.getId());
        ar.setCreateBy(loginUser.getId());

        log.info("验证提交预约信息：{}",ar);

        appointmentRecordService.addAppointmentRecord(ar);

        return "redirect:/";
    }


//    public String comfirmAppoint(AppointmentRecord appointmentRecord){
//
//    }

    @GetMapping("cancel_appoint")
//    @ResponseBody
    public String cancelAppoint(AppointmentRecord appointmentRecord){
        log.info("log:{}",appointmentRecord);
        appointmentRecordService.cancelAppointmentRecord(appointmentRecord);

        return "redirect:my_appoint";
    }
}
